package com.glorystone.ins.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.glorystone.ins.common.JsonResult;
import com.glorystone.ins.common.common.DataTable;
import com.glorystone.ins.common.common.PageVo;
import com.glorystone.ins.domain.Dictionary;
import com.glorystone.ins.interceptor.PageList;
import com.glorystone.ins.service.IDictionaryService;

@Controller
@RequestMapping("/sysdic")
public class DictionaryController extends BaseController {
	
	@Autowired
	private IDictionaryService dictionaryService;
	
	/**
	 * 去往数据字典管理列表
	 */
	@RequestMapping("/tolist")
	public ModelAndView tolist(ModelAndView mv){
		mv.setViewName("sysconfig/sysdic_list");
		return mv;
	}

	/**
	 * 组装数据字典列表数据
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/queryTableData",method = RequestMethod.GET)
	@ResponseBody
	public DataTable<Dictionary> queryTableData(HttpServletRequest request) {
		PageVo vo =  parametToPageVo(request,Dictionary.class);
		PageList<Dictionary> list = dictionaryService.findDictionaryList(vo);
		DataTable data = resultToDataTable(list);
		return data;
	}


	/**
	 * 去往新增-字典
	 */
	@RequestMapping("/toadd")
	public ModelAndView toAdd(ModelAndView mv){
		mv.setViewName("sysconfig/sysdic_add");
		return mv;
	}

	/**
	 * 去往编辑-字典
	 */
	@RequestMapping("/toedit/{id}")
	@ResponseBody
	public Dictionary toEdit(@PathVariable Integer id){
		Dictionary dictionary = dictionaryService.getDictionaryById(id);
		return dictionary;
	}

	/**
	 * 添加/编辑-字典
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody JsonResult addUser(Dictionary d) {
		try {
			Dictionary dic = dictionaryService.getDicByTypeAndRiskType(d);
			boolean result = true;
			if(d.getId() != null && d.getId() > 0){
				if(dic == null || dic.getId() == d.getId()){
					d.setUpdateTime(new Date());
					result = dictionaryService.updateDictionary(d);	
				}else{
					result = false;
				}
			}else{
				if(dic == null){
					d.setCreateTime(new Date());
					result = dictionaryService.addDictionary(d);
				}else{
					result = false;
				}
			}
			if(result){
				return JsonResult.createSuccessMsg("操作成功！");
			}else{
				return JsonResult.createErrorMsg("操作失败或者已经存在有字典类型和键值的记录！");
			}
		} catch (Exception e) {
			return JsonResult.createErrorMsg("操作失败或者已经存在有字典类型和键值的记录！");
		}
	}

	/**
	 * 删除字典
	 */
	@RequestMapping("/del/{id}")
	public @ResponseBody JsonResult delUser(@PathVariable Integer id){
		boolean result = dictionaryService.delDictionary(id);
		if(result){
			return returnSuccess("操作成功！");
		}else{
			return JsonResult.createErrorMsg("操作失败！");
		}
	}


}
